1
|
|
|
import type { primitive } from "ts-swiss.types" |
2
|
|
|
import type { BemInGeneral } from "./bem.types" |
3
|
|
|
|
4
|
|
|
const {isArray: $isArray} = Array |
5
|
|
|
|
6
|
|
|
let modDelimiter = "--" |
7
|
|
|
, elementDelimiter = "__" |
8
|
|
|
|
9
|
|
|
export type BemOptions = { |
10
|
|
|
elementDelimiter: string |
11
|
|
|
modDelimiter: string |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
export { |
15
|
|
|
bem2arr, |
16
|
|
|
setOptions |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
function bem2arr(query: BemInGeneral) { |
20
|
|
|
const $return: string[] = [] |
21
|
|
|
|
22
|
|
|
for (const base in query) { |
23
|
|
|
const baseQ = query[base] |
24
|
|
|
$return.push(base) |
25
|
|
|
|
26
|
|
|
if (!baseQ) |
27
|
|
|
continue |
28
|
|
|
if (typeof baseQ !== "object") { |
29
|
|
|
if (typeof baseQ === "string") |
30
|
|
|
$return.push(`${base}${modDelimiter}${baseQ}`) |
31
|
|
|
continue |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (!$isArray(baseQ)) |
35
|
|
|
$return.push(...mods2arr(base, baseQ)) |
36
|
|
|
else { |
37
|
|
|
const {length} = baseQ |
38
|
|
|
|
39
|
|
|
for (let i = 0; i < length; i++) { |
40
|
|
|
const modQ = baseQ[i] |
41
|
|
|
|
42
|
|
|
if (modQ === false) |
43
|
|
|
continue |
44
|
|
|
|
45
|
|
|
if (modQ !== null && typeof modQ === "object") |
46
|
|
|
$return.push(...mods2arr(base, modQ)) |
47
|
|
|
else |
48
|
|
|
$return.push(bmv(base, modQ)) |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $return |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function mods2arr(base: string, mods: Record<string, primitive>) { |
57
|
|
|
const classes = [] |
58
|
|
|
|
59
|
|
|
for (const mod in mods) { |
60
|
|
|
const value = mods[mod] |
61
|
|
|
if (value === false) |
62
|
|
|
continue |
63
|
|
|
|
64
|
|
|
classes.push(bmv(base, mod, value)) |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return classes |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
function bmv(base: string, mod: string, value: Exclude<primitive, false> = true) { |
71
|
|
|
return `${base}${modDelimiter}${mod}${ |
72
|
|
|
value === true |
73
|
|
|
? "" |
74
|
|
|
: `${modDelimiter}${value}` |
75
|
|
|
}` |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function setOptions({ |
79
|
|
|
elementDelimiter: elD = elementDelimiter, |
80
|
|
|
modDelimiter: modDel = modDelimiter |
81
|
|
|
}: Partial<BemOptions>) { |
82
|
|
|
modDelimiter = modDel |
83
|
|
|
elementDelimiter = elD |
84
|
|
|
} |
85
|
|
|
|